
import java.awt.image.BufferedImage;
import javax.imageio.ImageWriter;
import javax.imageio.ImageIO;
import java.util.Iterator;
import java.util.zip.ZipException;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;

/**
 * MakePngs.java
 *
 *
 * Created: Wed Jul 11 16:25:18 2007
 *
 * @author <a href="mailto:fseibel@sprintmail.com">Fred Seibel</a>
 * @version 1.0
 */
public class MakePng {

	Placemark placemark;

	public void makePng(ZipOutputStream zipout, int i) throws IOException, ZipException
	{
		try {
			ZipEntry entry = new ZipEntry(placemark.getName()+i +".png"); // Make a ZipEntry
			zipout.putNextEntry(entry); // Store entry
			zipout.flush();
			BufferedImage image = placemark.getImage(i);

			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ImageWriter imageWriter = null;
			for (Iterator<ImageWriter> iw = ImageIO.getImageWritersBySuffix("png");iw.hasNext();) {
				imageWriter = iw.next();
			}
			MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos);

			imageWriter.setOutput(mcios);
			imageWriter.write(image);

			//System.err.println(makepngs.baos.size() + "\tMakePngs.java @ 68"); //Debug
			for (byte b : baos.toByteArray()) {
				zipout.write(b);
			}
			zipout.flush();
		}
		catch(ZipException zexp){
			System.out.println("ZipException encountered: " + zexp.getMessage());
		}
	}

	public void addPngToZip(ZipOutputStream zipout, int i) throws IOException
	{
//		//for (int i = 0; i < 128; i++)
//		//{
//		ZipEntry entry = new ZipEntry(placemark.getName()+i +".png"); // Make a ZipEntry
//		zipout.putNextEntry(entry); // Store entry
//		zipout.flush();
//		BufferedImage image = placemark.getImage(i);
//		makePng(zipout, i);
//		//System.err.println(makepngs.baos.size() + "\tMakePngs.java @ 68"); //Debug
//		for (byte b : this.baos.toByteArray()) {
//		zipout.write(b);
//		}
//		zipout.flush();
		makePng(zipout, i);

	}

	public void addPngsToZip (ZipOutputStream zipout) throws IOException {
		for (int i = 0; i < placemark.getSize(); i++)
		{
			makePng(zipout, i);
		}
	}

	public MakePng(Placemark placemark) {
		super();
		this.placemark = placemark;
//		this.baos = new ByteArrayOutputStream();
	} 

} // MakePngs